/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bml.client.secret.demo;

import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: baiml
 * @Title: Sch
 * @ProjectName: 作业管理平台
 * @Depart 软件事业部
 * @Description:
 * @date: 2021/11/29 11:10
 */
public class Sch {

    public static void main(String[] args) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(12);
        taskScheduler.setThreadNamePrefix("pool");
        // 线程池对拒绝任务（无线程可用）的处理策略，目前只支持AbortPolicy、CallerRunsPolicy；默认为后者
        taskScheduler.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 调度器shutdown被调用时等待当前被调度的任务完成
        taskScheduler.setWaitForTasksToCompleteOnShutdown(true);
        // 等待时长
        taskScheduler.setAwaitTerminationSeconds(60);
        taskScheduler.initialize();
        ScheduledExecutorService scheduledExecutor = taskScheduler.getScheduledExecutor();
        List<Runnable> runnables = scheduledExecutor.shutdownNow();
        System.out.println(runnables);


        scheduledExecutor.scheduleAtFixedRate(()->{
            System.out.println("111");
        },1,1, TimeUnit.SECONDS);
        new Thread(()->{
            for (;;){
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(scheduledExecutor.isShutdown());
            }
        }).start();
        System.out.println(scheduledExecutor.shutdownNow());
    }
}
